package com.work.music.dao;

import com.work.music.entity.WordInfo;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

@Repository
public interface WordInfoMapper {
    @Delete({
        "delete from music_word_info",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
        "insert into music_word_info (word, returnPhrase, ",
        "query, translation, ",
        "errorCode, l, tSpeakUrl, ",
        "web, dict, ",
        "webdict, basic, ",
        "speakUrl)",
        "values (#{word,jdbcType=VARCHAR}, #{returnphrase,jdbcType=VARCHAR}, ",
        "#{query,jdbcType=VARCHAR}, #{translation,jdbcType=VARCHAR}, ",
        "#{errorcode,jdbcType=VARCHAR}, #{l,jdbcType=VARCHAR}, #{tspeakurl,jdbcType=LONGVARCHAR}, ",
        "#{web,jdbcType=LONGVARCHAR}, #{dict,jdbcType=LONGVARCHAR}, ",
        "#{webdict,jdbcType=LONGVARCHAR}, #{basic,jdbcType=LONGVARCHAR}, ",
        "#{speakurl,jdbcType=LONGVARCHAR})"
    })
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Long.class)
    int insert(WordInfo record);

    @Select({
        "select",
        "id, word, returnPhrase, query, translation, errorCode, l, tSpeakUrl, web, dict, ",
        "webdict, basic, speakUrl",
        "from music_word_info",
        "where id = #{id,jdbcType=BIGINT}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="word", property="word", jdbcType=JdbcType.VARCHAR),
        @Result(column="returnPhrase", property="returnphrase", jdbcType=JdbcType.VARCHAR),
        @Result(column="query", property="query", jdbcType=JdbcType.VARCHAR),
        @Result(column="translation", property="translation", jdbcType=JdbcType.VARCHAR),
        @Result(column="errorCode", property="errorcode", jdbcType=JdbcType.VARCHAR),
        @Result(column="l", property="l", jdbcType=JdbcType.VARCHAR),
        @Result(column="tSpeakUrl", property="tspeakurl", jdbcType=JdbcType.LONGVARCHAR),
        @Result(column="web", property="web", jdbcType=JdbcType.LONGVARCHAR),
        @Result(column="dict", property="dict", jdbcType=JdbcType.LONGVARCHAR),
        @Result(column="webdict", property="webdict", jdbcType=JdbcType.LONGVARCHAR),
        @Result(column="basic", property="basic", jdbcType=JdbcType.LONGVARCHAR),
        @Result(column="speakUrl", property="speakurl", jdbcType=JdbcType.LONGVARCHAR)
    })
    WordInfo selectByPrimaryKey(Long id);

    @Select({
        "select",
        "id, word, returnPhrase, query, translation, errorCode, l, tSpeakUrl, web, dict, ",
        "webdict, basic, speakUrl",
        "from music_word_info"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="word", property="word", jdbcType=JdbcType.VARCHAR),
        @Result(column="returnPhrase", property="returnphrase", jdbcType=JdbcType.VARCHAR),
        @Result(column="query", property="query", jdbcType=JdbcType.VARCHAR),
        @Result(column="translation", property="translation", jdbcType=JdbcType.VARCHAR),
        @Result(column="errorCode", property="errorcode", jdbcType=JdbcType.VARCHAR),
        @Result(column="l", property="l", jdbcType=JdbcType.VARCHAR),
        @Result(column="tSpeakUrl", property="tspeakurl", jdbcType=JdbcType.LONGVARCHAR),
        @Result(column="web", property="web", jdbcType=JdbcType.LONGVARCHAR),
        @Result(column="dict", property="dict", jdbcType=JdbcType.LONGVARCHAR),
        @Result(column="webdict", property="webdict", jdbcType=JdbcType.LONGVARCHAR),
        @Result(column="basic", property="basic", jdbcType=JdbcType.LONGVARCHAR),
        @Result(column="speakUrl", property="speakurl", jdbcType=JdbcType.LONGVARCHAR)
    })
    List<WordInfo> selectAll();

    @Update({
        "update music_word_info",
        "set word = #{word,jdbcType=VARCHAR},",
          "returnPhrase = #{returnphrase,jdbcType=VARCHAR},",
          "query = #{query,jdbcType=VARCHAR},",
          "translation = #{translation,jdbcType=VARCHAR},",
          "errorCode = #{errorcode,jdbcType=VARCHAR},",
          "l = #{l,jdbcType=VARCHAR},",
          "tSpeakUrl = #{tspeakurl,jdbcType=LONGVARCHAR},",
          "web = #{web,jdbcType=LONGVARCHAR},",
          "dict = #{dict,jdbcType=LONGVARCHAR},",
          "webdict = #{webdict,jdbcType=LONGVARCHAR},",
          "basic = #{basic,jdbcType=LONGVARCHAR},",
          "speakUrl = #{speakurl,jdbcType=LONGVARCHAR}",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(WordInfo record);

    /**
     * 根据单词获取单词详情
     * @param word
     * @return
     */
    @Select({
            "select",
            "id, word, returnPhrase, query, translation, errorCode, l, tSpeakUrl, web, dict, ",
            "webdict, basic, speakUrl",
            "from music_word_info",
            "where word = #{word,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="word", property="word", jdbcType=JdbcType.VARCHAR),
            @Result(column="returnPhrase", property="returnphrase", jdbcType=JdbcType.VARCHAR),
            @Result(column="query", property="query", jdbcType=JdbcType.VARCHAR),
            @Result(column="translation", property="translation", jdbcType=JdbcType.VARCHAR),
            @Result(column="errorCode", property="errorcode", jdbcType=JdbcType.VARCHAR),
            @Result(column="l", property="l", jdbcType=JdbcType.VARCHAR),
            @Result(column="tSpeakUrl", property="tspeakurl", jdbcType=JdbcType.LONGVARCHAR),
            @Result(column="web", property="web", jdbcType=JdbcType.LONGVARCHAR),
            @Result(column="dict", property="dict", jdbcType=JdbcType.LONGVARCHAR),
            @Result(column="webdict", property="webdict", jdbcType=JdbcType.LONGVARCHAR),
            @Result(column="basic", property="basic", jdbcType=JdbcType.LONGVARCHAR),
            @Result(column="speakUrl", property="speakurl", jdbcType=JdbcType.LONGVARCHAR)
    })
    WordInfo selectByWordName(String word);

    /**
     * 根据用户id获取用户收藏的单词信息
     * @param uid
     * @param start
     * @param count
     * @return
     */
    @Select("select " +
            "id, word, returnPhrase, query, translation, errorCode, l, tSpeakUrl, web, dict, " +
            "webdict, basic, speakUrl " +
            "from " +
            "music_word_info " +
            "where " +
            "id in (select typeid from music_collect_praise where uid=#{uid} and type=4 and peration=1) " +
            "order by word limit #{start},#{count}")
    List<Map<String, Object>> selectByUid(@Param("uid") long uid, @Param("start") int start, @Param("count") int count);
}